package com.hyg.lib_music.ui.fragment;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.Base.BaseApplication;
import com.hyg.lib_common.DataModel.Music.MusicCategoryData;
import com.hyg.lib_common.Net.CommonHttpUtil;
import com.hyg.lib_music.R;
import com.hyg.lib_music.adapter.MusicCategoryItemViewBinder;
import com.hyg.lib_music.adapter.MusicCategoryTitleViewBinder;
import com.hyg.lib_music.adapter.MusicCategoryViewBinder;
import com.hyg.lib_music.adapter.MusicCategotyAdapter;
import com.hyg.lib_music.modle.NewMusicCategoryData;
import com.hyg.lib_music.presenter.MusicCategoryListContract;
import com.hyg.lib_music.presenter.di.components.DaggerActivityComponent;
import com.hyg.lib_music.service.MusicService;
import com.hyg.lib_music.ui.activity.MainMusicBedActivity;
import com.hyg.lib_music.ui.activity.MusicKnowledgeActivity;
import com.hyg.lib_music.util.ApplicationComponentUtil;
import com.hyg.lib_music.util.MusicChangeEvent;

import org.greenrobot.eventbus.Subscribe;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static me.drakeet.multitype.MultiTypeAsserts.assertAllRegistered;
import static me.drakeet.multitype.MultiTypeAsserts.assertHasTheSameAdapter;

//音乐库
public class MusicCategoryListFragment extends BaseFragment {

    public static Context mContext;
    private static final int SPAN_COUNT = 2;

    private MusicCategotyAdapter mAdapter;
    BaseActivity activity = (BaseActivity) getActivity();
    ArrayList<NewMusicCategoryData.DataDTO> list = new ArrayList<>();
    RecyclerView mRecyclerView;
    Button KnowladgeBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music_library, container, false);
        mContext = this.getActivity();
        activity = (BaseActivity) getActivity();
        initView(view);
        getCategoryData();
        return view;
    }

    private void initView(View view) {
        mRecyclerView = view.findViewById(R.id.recycler_view);
        KnowladgeBtn = view.findViewById(R.id.iv_knowledge);
        KnowladgeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MusicKnowledgeActivity.class));
            }
        });
    }

    public void getCategoryData() {
        CommonHttpUtil.getMusicCategoryData(new Callback() {
            @Override
            public void onFailure(Call call, IOException e){
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        activity.ErrorDialog("连接服务器超时，请稍后重试");
                    }
                });
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result=response.body().string();
                Log.i("lgb", "onResponse:CategoryList = "+result);
                parseCategotyData(result);
            }
        });


    }

    private void parseCategotyData(String  result){
        NewMusicCategoryData data = JSONObject.parseObject(result, NewMusicCategoryData.class);
        if (data.code == 200) {
            list = (ArrayList<NewMusicCategoryData.DataDTO>) data.data;
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
//                    final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(SPAN_COUNT, OrientationHelper.VERTICAL);
                    mRecyclerView.setLayoutManager(layoutManager);
                    mAdapter = new MusicCategotyAdapter(getActivity(), list);
                    mRecyclerView.setAdapter(mAdapter);
                }
            });
        } else {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    activity.ErrorDialog(data.message);
                }
            });
        }
    }


}